using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Nuwanda.TfsSpotlight.Shell;
using Nuwanda.TfsSpotlight.TFS;
using Nuwanda.TfsSpotlight.Views.GoToWorkItem;
using Nuwanda.TfsSpotlight.Views.Servers;
using Nuwanda.TfsSpotlight.Views.WorkItemHistory;
using StructureMap;
using StructureMap.Configuration.DSL;

namespace Nuwanda.TfsSpotlight
{
    public partial class TfsSpotlightApplication : ApplicationController
    {
        private const string APPLICATION_NAME_FINAL = "TfsSpotlight v{0} by CodeGears";
        private const string APPLICATION_NAME_BETA = "TfsSpotlight v{0} BETA by CodeGears";

        public override string ApplicationName
        {
            get 
            { 
                return this.IsBeta ?
                    String.Format(APPLICATION_NAME_BETA, Application.ProductVersion) :
                    String.Format(APPLICATION_NAME_FINAL, Application.ProductVersion);
            }
        }

        public override Icon ApplicationIcon
        {
            get { return Properties.Resources.ApplicationIcon; }
        }

        protected override void OnInitialization()
        {
            SetUpCommands();
            SetUpLeftSideBar();
            SetUpRightSideBar();
            SetUpGoToWorkItemToolStripView();
        }

        private void SetUpGoToWorkItemToolStripView()
        {
            var configurator = this.MenuService.ConfigureCustomToolStrip(CommandsNames.GoToWorkItem);
            configurator.AddSeparator();
            configurator.AddLabel("Search:");
            var textBox = configurator.AddTextBox();
            var dropDown = configurator.AddDropDownButton(Properties.Resources.zoom_16x16);
            var goToWorkItemView = new GoToWorkItemView(dropDown, textBox);

            ObjectFactory.Configure(
                registry => registry.BuildInstancesOf<IGoToWorkItemView>()
                .TheDefaultIs(goToWorkItemView).AsSingletons());

            var presenter = ObjectFactory.GetInstance<IGoToWorkItemPresenter>();
            presenter.Initialize();
        }

        protected override void OnRun()
        {
            this.MenuService.InvalidateCommands();
            this.MenuService.RunCommand(CommandsNames.AutoConnect);
            //this.UpdateService.CheckForUpdates();
        }

        protected override void OnClosed()
        {
            
        }

        protected override bool OnClosing()
        {
            this.MenuService.RunCommand(CommandsNames.ExitApplication);
            return true;
        }

        protected override void OnSettingDependencies(Registry registry)
        {
            registry.ScanAssemblies().IncludeAssembly("TfsSpotlight");
            registry.BuildInstancesOf<ICredentialsProvider>()
                .TheDefaultIsConcreteType<NotifyAuthenticationNeededCredencialsProvider>()
                .AsSingletons();
        }

        private void SetUpLeftSideBar()
        {
            this.LeftSideBar.Text = "Servers";
            this.LeftSideBar.Icon = Properties.Resources.Server;

            var serversPresenter = ObjectFactory.GetInstance<IServersPresenter>();
            serversPresenter.Initialize();
            this.LeftSideBar.SetContent(serversPresenter.ViewControl);
        }

        private void SetUpRightSideBar()
        {
            this.RightSidebar.Text = "History";
            this.RightSidebar.Icon = Properties.Resources.History;

            var historyPresenter = ObjectFactory.GetInstance<IHistoryPresenter>();
            historyPresenter.Initialize();
            this.RightSidebar.SetContent(historyPresenter.ViewConrol);
        }
    }
}